from rest_framework import serializers
from apps.UMM.models import Attendance, User


class AttendanceSerializer(serializers.ModelSerializer):
    """工时记录详细序列化器"""
    
    class Meta:
        model = Attendance
        fields = '__all__'
        
    def to_representation(self, instance):
        """重写返回格式，添加关联对象的显示字段"""
        ret = super().to_representation(instance)
        
        # 添加员工昵称显示字段
        if instance.nickname:
            ret['nickname_display'] = instance.nickname.nickname
            
        return ret


class AttendanceListSerializer(serializers.ModelSerializer):
    """工时记录列表序列化器"""
    nickname_display = serializers.SerializerMethodField()
    
    class Meta:
        model = Attendance
        fields = [
            'id', 'nickname', 'nickname_display', 'login_time', 'logout_time', 
            'online_duration', 'duty_type', 'duty_date', 'hourly_wage'
        ]
        
    def get_nickname_display(self, obj):
        """获取员工昵称"""
        return obj.nickname.nickname if obj.nickname else "" 